package controllers;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trackvoodoo.models.UserDocument.User;

import context.UserContext;

import datebase.live.access.UserDAO;

import models.UserModel;

/**
 * Servlet implementation class Index_SL
 */
public class Index_SL extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private enum ACTION {PROFILE, TRACKDAYS, NEWS, FORUM, SHOP, CARS};
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Index_SL() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("choice");
		String userName = request.getParameter("username");
		System.out.println(action + " " + userName);
		
		//Switch on the choice.
		ACTION actionEnum = ACTION.valueOf(action.toUpperCase());
		String pageAddress = actionEnum.toString().toLowerCase() + ".jsp";
		RequestDispatcher newView = request.getRequestDispatcher(pageAddress);
		switch (actionEnum)
		{
			case PROFILE : 
							if (UserContext.instance.isStillValid(userName))
							{
								try {
									User userDetails = UserDAO.getAllUserInfo(userName);
									request.setAttribute("userDetails", userDetails);
								} catch (SQLException e) {
									System.out.println("Cannot find user " +userName);
								}
								
							}
							
							break;
			case TRACKDAYS : 
							break;
			case NEWS : 
							break;
			case FORUM : 
							break;
			case SHOP : 
							break;
			case CARS :
							break;
							
			default : break;
		}

		request.setAttribute("username", userName);
		newView.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//For login
		String user = request.getParameter("username");
		String pass = request.getParameter("password");
		
		// Check if the user exists and login?		
		if (UserDAO.validatePassword(user, pass))
		{
			//Add user to the context
			UserContext.instance.addUser(user);
			
			request.setAttribute("userName", user);
			request.setAttribute("valid", true);
			
			RequestDispatcher newView = request.getRequestDispatcher("index.jsp");
			newView.forward(request, response);
		}
		else
		{
			request.setAttribute("errorMessage", "Incorrect User or Password");
			RequestDispatcher newView = request.getRequestDispatcher("index.jsp");
			newView.forward(request, response);

		}
		
		
		
	}

}
